package com.zbl.zblproject.core.controller;

import com.alibaba.fastjson.JSONObject;
import com.zbl.jdbc.ConManager2;
import com.zbl.zblproject.core.entity.AdminUser;
import com.zbl.zblproject.core.entity.Result;
import com.zbl.zblproject.core.entity.UserAddress;
import com.zbl.zblproject.core.mapper.AdminUserMapper;
import com.zbl.zblproject.core.mapper.UserAddressMapper;
import com.zbl.zblproject.core.service.UserService;
import com.zbl.zblproject.core.utils.PasswordUtil;
import com.zbl.zblproject.core.utils.ResultUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;


/**
 * @author youlanqiang
 * @version 1.0
 * @date 2018/11/13
 * 用户接口
 * 用户模糊查询
 */
@RestController
@RequestMapping("/user")
@Api(tags = "用户接口")
public class UserController {


    @Value("${cert-path}")
    private String certPath;


    @Autowired
    private UserService userService;

    @Autowired
    private AdminUserMapper adminUserMapper;

    @Autowired
    private UserAddressMapper userAddressMapper;


    @GetMapping("/getvipinfo/{mobile}")
    @ApiOperation("获取泰格系统会员积分信息")
    public Result getvipinfo(@ApiParam("用户手机号") @PathVariable String mobile)
    {
           String jf= ConManager2.selectByte(mobile,mobile);
           if("-1".equals(jf)){
               return ResultUtil.error("no user");
           }
           RVO r1=new RVO();
           r1.setIntegral(jf);

        return ResultUtil.success("success",r1);
    }


    @GetMapping("/list")
    @ApiOperation("用户列表")
    public Result list() {
        return ResultUtil.success("成功", userService.list());
    }

    @PostMapping("/update/{id}")
    @ApiOperation("更新用户信息")
    public Result update(@ApiParam("用户ID") @PathVariable int id,
                         @ApiParam("用户名称") @RequestParam("userName") String name,
                         @ApiParam("地址") @RequestParam("address") String address,
                         @ApiParam("电话") @RequestParam("mobile") String mobile,
                         @ApiParam("email") @RequestParam("email") String email,
                         @ApiParam("logistics") @RequestParam("logistics") String logistics,
                         @ApiParam("用户编号") @RequestParam("userCode") String userCode) {
        AdminUser user = adminUserMapper.selectUserById(id);
        if (user == null) {
            return ResultUtil.error("用户不存在!");
        }
        user.setUserName(name);
        user.setEmail(email);
        user.setUserCode(userCode);
        user.setLogistics(logistics);

        UserAddress userAddress = userAddressMapper.selectOneByUserId(user.getUserId());
        if (userAddress != null) {
            userAddress.setAddress(address);
            userAddress.setMobile(mobile);
            userAddressMapper.updateById(userAddress);
        }else{
            userAddress = new UserAddress();
            userAddress.setMobile(mobile);
            userAddress.setAddress(address);
            userAddress.setUserId(user.getUserId());
            userAddressMapper.insert(userAddress);
        }
        if(adminUserMapper.updateById(user) == 1){
            return ResultUtil.success("成功!");
        }
        return ResultUtil.error("失败!");
    }


    @PostMapping("/login")
    @ApiOperation("登陆认证")
    public Result login(@ApiParam("用户名") @RequestParam("name") String userName,
                        @ApiParam("密码") @RequestParam("password") String password) {
        AdminUser user = adminUserMapper.selectOneByName(userName);

        if (user == null) {
            return ResultUtil.error("用户不存在!");
        }

        if (PasswordUtil.isPassword(password, user)) {
            JSONObject result = new JSONObject();
            result.put("user_id", user.getUserId());
            result.put("user_name", user.getUserName());
            result.put("user_code", user.getUserCode());
            return ResultUtil.success("登陆成功!", result);
        }

        return ResultUtil.error("登陆失败! 密码错误!");
    }

    @GetMapping("/get-number/{id}")
    @ApiOperation("获取用户编号")
    public Result getUserNumber(@ApiParam("用户ID") @PathVariable int id) {
        String number = adminUserMapper.selectUserNumber(id);
        return ResultUtil.success("成功!", number);
    }


    @GetMapping("/search")
    @ApiOperation("查询用户")
    public Result searchUser(@ApiParam("名称") @RequestParam("name") String name) {
        return ResultUtil.success("成功", userService.searchUser(name));
    }


    @GetMapping("/recharge-log/{userId}/{current}/{size}")
    @ApiOperation("充值记录")
    public Result rechargeLog(@ApiParam("用户ID") @PathVariable int userId,
                              @ApiParam("页数") @PathVariable int current,
                              @ApiParam("数量") @PathVariable int size) {
        return ResultUtil.success("成功", userService.logPage(userId, current, size));
    }

    @GetMapping("/recharge-log/{current}/{size}")
    @ApiOperation("所有用户的充值记录")
    public Result rechargeLogOfAll(@ApiParam("页数") @PathVariable int current,
                                   @ApiParam("数量") @PathVariable int size,
                                   @ApiParam("开始时间") @RequestParam String start,
                                   @ApiParam("结束时间") @RequestParam String end) {
        return ResultUtil.success("成功", userService.logPage(current, size, start, end));
    }


    @PostMapping("/recharge")
    @ApiOperation("用户充值")
    public Result recharge(@ApiParam("用户ID") @RequestParam("userId") int userId,
                           @ApiParam("操作人ID") @RequestParam("operatorId") int operatorId,
                           @ApiParam("充值金额") @RequestParam("price") double price) {
        try {
            if (userService.recharge(userId, operatorId, price)) {
                return ResultUtil.success("成功!");
            }
            return ResultUtil.error("失败");
        } catch (RuntimeException e) {
            return ResultUtil.error(e.getMessage());
        }
    }
}